package com.woniuxy.background.advice;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.houbb.sensitive.core.api.SensitiveUtil;
import com.woniuxy.background.pojo.Admin;
import com.woniuxy.background.pojo.Log;
import com.woniuxy.background.service.AdminService;
import com.woniuxy.background.service.LogService;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.CodeSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Component
@Aspect
public class LogAdvice {

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private LogService logService;

    @Autowired
    private AdminService adminService;


    @Before(value = "@annotation(SysLog)")
    public void log(JoinPoint joinPoint){
        this.saveLog(joinPoint,getMethodDesc(joinPoint));
    }



    //记录登录日志
    @AfterReturning(value = "execution(* com.woniuxy.background.controller.AdminController.subLogin(..))")
    public void loginLog(JoinPoint joinPoint){
         this.saveLog(joinPoint,"系统用户登录");
    }




    private void saveLog(JoinPoint joinPoint,String desc){
        Admin admin = new Admin();
        admin.setUsername(SecurityUtils.getSubject().getPrincipal().toString());
        admin = adminService.selectAdminByName(admin);
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        Log log = new Log();
        log.setIp(request.getRemoteHost());
        try {
            List params = Arrays.asList(joinPoint.getArgs());
            System.out.println("???"+params);
            List result = null;
            try {
                result = SensitiveUtil.desJsonCollection(params);
                log.setParams(objectMapper.writeValueAsString(result));
            }catch (NullPointerException e){
                e.printStackTrace();
                log.setParams(objectMapper.writeValueAsString(params));
            }
            log.setParams(objectMapper.writeValueAsString(result));
            log.setMethod(joinPoint.getSignature().getName());
            log.setCreateTime(new Date());
            log.setUsername(admin.getUsername());
            log.setOperation(desc);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            log.setParams(null);
        }
        //将日志信息添加到数据库中
          logService.saveLog(log);
    }


    public String getMethodDesc(JoinPoint joinPoint){
        Class target= joinPoint.getTarget().getClass();
        //获取连接点的方法签名
        CodeSignature signature = (CodeSignature)joinPoint.getSignature();
        //根据方法名称获取某个方法，参数2是方法的参数列表类型。
        try {
            Method method = target.getMethod(joinPoint.getSignature().getName(),signature.getParameterTypes());
            String methodDesc = method.getAnnotation(SysLog.class).value();
            return methodDesc;
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return null;
    }

}
